$(document).ready(function () {
    getData()
});

function getData(){
    $.ajax({
        url: global.apiUrl + '/admin_get_community_phone',
        type: 'GET',
        dataType: 'json',
        data: {
            community_id: global.community_id
        },
        header: {
            'content-type': 'application/json' //默认值
        },
        success: function(res){
            // console.log(typeof(res));
            var html = ''
            for(var i in res){
                html += `<div class="phoneItemAre">
                            <div class="phoneAre">
                                <span>名称：${res[i].phone_name}</span>
                                <span>电话：${res[i].phone}</span>
                            </div>
                            <div class="operationAre themeColor">
                                <div class="operartionItem delete" data-id="${res[i].id}">
                                    <i class="fa fa-close" style="width: 16px;height: 16px;margin-right: 10px"></i>
                                    <span>删除</span>
                                </div>
                                <div class="horizontal"></div>
                                <div class="operartionItem editor" data-name="${res[i].phone_name}" data-phone="${res[i].phone}" data-id="${res[i].id})" >
                                    <i class="fa fa-pencil-square-o" style="width: 18px;height: 16px;margin-right: 10px"></i>
                                    <span class="editorTxt">修改</span>
                                </div>
                            </div>
                            <div class="line"></div>
                        </div>`
            }
            $('#phoneMainAre').html(html)
        }
    })
}

var isEditor = false
function clickBtn(){
    let name =  $("#name").val();
    let phone = $("#phone").val();
    if(name){
        let type = 'name'
        if(!validation(name,type)){
            alert("请填写正确名称")
            return
        }
    }else{
        alert("请填写名称")
        return
    }
    if(phone){
        let type = 'number'
        if(!validation(phone,type)){
            alert("请填写正确电话")
            return
        }
    }else{
        alert("请填写电话")
        return
    }
    setData(name,phone,'')
}

var phoneId = ''
//编辑

$('#phoneMainAre').on('click','.editor',function(){
    var id = $(this).data('id');
    var name = $(this).data('name');
    var phone = $(this).data('phone');
    if(isEditor){
        isEditor = false;
        $('#text').text('添加热线');
        $(this).find('.editorTxt').text('修改');
        $("#name").val('');
        $("#phone").val('');
        phoneId = ''
    }else{
        isEditor = true;
        $('#text').text('修改热线');
        $(this).find('.editorTxt').text('取消');
        $("#name").val(name)
        $("#phone").val(phone)
        phoneId = id
    }
})
$('#phoneMainAre').on('click','.delete',function(){
    var id = $(this).data('id');
    $.ajax({
        url: global.apiUrl + '/admin_delete_community_phone',
        type: 'GET',
        dataType: 'json',
        data: {
            id: id,
        },
        header: {
            'content-type': 'application/json' //默认值
        },
        success: function(res){
            console.log(res)
            if(res == '1'){
                isEditor = false;
                $('#text').text('添加热线');
                $("#name").val('');
                $("#phone").val('');
                phoneId = ''
                getData()
                alert('删除成功')
            }else{
                alert('删除失败')
            }
        }
    })
})

//设置数据
function setData(name,phone,id){
    if(isEditor){
        id = phoneId
    }
    $.ajax({
        url: global.apiUrl + '/admin_operation_community_phone',
        type: 'GET',
        dataType: 'json',
        data: {
            community_id: global.community_id,
            phone: phone,
            phone_name: name,
            id: id,
        },
        header: {
            'content-type': 'application/json' //默认值
        },
        success: function(res){
            console.log(res)
            if(res == '1'){
                isEditor = false;
                $('#text').text('添加热线');
                $("#name").val('');
                $("#phone").val('');
                phoneId = ''
                getData()
                alert('保存成功')
            }else{
                alert('保存失败')
            }
        }
    })
}
